<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.docNumber" clearable size="mini" class="filter-item" style="width: 200px;" placeholder="请输入文件编号"/>
      <el-input v-model="listQuery.docName" clearable size="mini" class="filter-item" style="width: 200px;" placeholder="请输入文件名称"/>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button :loading="downloadLoading" size="mini" class="filter-item" type="warning" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="table-expand">
            <el-form-item label="起草时间/起草人">
              <span>{{ props.row.draftsTime }}</span><span>{{ props.row.addTime }}</span>
            </el-form-item>
            <el-form-item label="审核时间/审核人">
              <span>{{ props.row.reviewTime }}</span><span>{{ props.row.reviewPerson }}</span>
            </el-form-item>
            <el-form-item label="审批时间/审批人">
              <span>{{ props.row.certifyTime }}</span><span>{{ props.row.certifyPerson }}</span>
            </el-form-item>
            <el-form-item label="发放部门">
              <span>{{ props.row.provideId }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column align="center" min-width="110" label="文档编号" prop="docNumber" sortable/>

      <el-table-column align="center" min-width="200" label="文档名称" prop="docName" />

      <el-table-column align="center" min-width="200" label="创建时间" prop="addTime" />
      <el-table-column align="center" label="原文查看" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="100px" label="文档类型" prop="docType">
        <template slot-scope="scope">{{ scope.row.docType | formatDocType }}</template>
      </el-table-column>
      <el-table-column align="center" label="是否有效" prop="isEffective">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isEffective ? 'success' : 'error' ">{{ scope.row.isEffective ? '无效' : '有效' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>

  </div>
</template>

<style>
	.el-dialog {
    width: 60%;
	}
  .table-expand {
    font-size: 0;
  }
  .table-expand label {
    width: 100px;
    color: #99a9bf;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
  .gallery {
    width: 80px;
    margin-right: 10px;
  }
</style>

<script>

import { listDocs, deleteDocs } from '@/api/business/doc'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'DocList',
  components: { BackToTop, Pagination },
  filters: {
    formatDocType(docType) {
      if (docType === 1) {
        return '操作规程'
      } else if (docType === 2) {
        return '管理文件'
      } else {
        return '规章制度'
      }
    },
    formatPerson(personId) {
      if (personId === 0) {
        return '正常'
      } else if (personId === 1) {
        return '已过期'
      } else {
        return '已下架'
      }
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        docNumber: undefined,
        docName: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      docDetail: '',
      detailDialogVisible: false,
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listDocs(this.listQuery).then(response => {
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({ path: '/doc/create' })
    },
    handleUpdate(row) {
      this.$router.push({ path: '/doc/edit', query: { id: row.id }})
    },
    showDetail(detail) {
      this.docDetail = detail
      this.detailDialogVisible = true
    },
    handleDelete(row) {
      deleteDocs(row).then(response => {
        this.$notify.success({
          title: '成功',
          message: '删除成功'
        })
        const index = this.list.indexOf(row)
        this.list.splice(index, 1)
      }).catch(response => {
        this.$notify.error({
          title: '失败',
          message: response.data.errmsg
        })
      })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['文档序号', '文档编号', '名称', '文档类型', '审核时间', '批准时间', '发放部门', '是否有效', '是否删除']
        const filterVal = ['id', 'docNumber', 'docName', 'docType', 'addTime', 'isNew', 'reviewTime', 'keywords', 'isEffective', 'deleted']
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '文档信息')
        this.downloadLoading = false
      })
    }
  }
}
</script>
